package com.theone.module.demo.service.testone;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.theone.module.demo.controller.admin.testone.vo.*;
import com.theone.module.demo.dal.dataobject.testone.TestOneDO;
import com.theone.framework.common.pojo.PageResult;

import com.theone.module.demo.convert.testone.TestOneConvert;
import com.theone.module.demo.dal.mysql.testone.TestOneMapper;

import static com.theone.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.theone.module.demo.enums.ErrorCodeConstants.*;

/**
 * 第一个测试 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class TestOneServiceImpl implements TestOneService {

    @Resource
    private TestOneMapper testOneMapper;

    @Override
    public Long createTestOne(TestOneCreateReqVO createReqVO) {
        // 插入
        TestOneDO testOne = TestOneConvert.INSTANCE.convert(createReqVO);
        testOneMapper.insert(testOne);
        // 返回
        return testOne.getId();
    }

    @Override
    public void updateTestOne(TestOneUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateTestOneExists(updateReqVO.getId());
        // 更新
        TestOneDO updateObj = TestOneConvert.INSTANCE.convert(updateReqVO);
        testOneMapper.updateById(updateObj);
    }

    @Override
    public void deleteTestOne(Long id) {
        // 校验存在
        this.validateTestOneExists(id);
        // 删除
        testOneMapper.deleteById(id);
    }

    private void validateTestOneExists(Long id) {
        if (testOneMapper.selectById(id) == null) {
            throw exception(TEST_ONE_NOT_EXISTS);
        }
    }

    @Override
    public TestOneDO getTestOne(Long id) {
        return testOneMapper.selectById(id);
    }

    @Override
    public List<TestOneDO> getTestOneList(Collection<Long> ids) {
        return testOneMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<TestOneDO> getTestOnePage(TestOnePageReqVO pageReqVO) {
        return testOneMapper.selectPage(pageReqVO);
    }

    @Override
    public List<TestOneDO> getTestOneList(TestOneExportReqVO exportReqVO) {
        return testOneMapper.selectList(exportReqVO);
    }

}
